package com.stsdai.shop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stsdai.common.pojo.StsdaiResult;
import com.stsdai.manager.pojo.Item;
import com.stsdai.manager.pojo.User;
import com.stsdai.manager.service.ItemService;
import com.stsdai.sso.service.UserService;

@Controller
@RequestMapping("item")
public class ItemController {

	@Autowired
	private ItemService itemService;

	@Autowired
	private UserService userService;

	// type: "POST",
	// url: "/rest/item",
	/**
	 * 新增商品
	 * 
	 * @param item
	 * @param desc
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody // 必须要加
	public void saveItem(@CookieValue(value = "STSD_TICKET",defaultValue = "MyJsessionId") String ticket, Item item, String desc) {
		// 通过ticket获取用户
		Long shopid = null;
		User user = userService.queryUserByTicket(ticket);
		// 设置商品的卖家id
		item.setShopid(user.getId());
		this.itemService.saveItem(item, desc);
	}

	/**
	 * 根据shopid分页查询商品
	 * 
	 * @param page
	 * @param page
	 * @param rows
	 * @return
	 */
	// url:'/rest/item',method:'get',pageSize:30,
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public StsdaiResult<Item> queryItemByPage(@CookieValue(value = "STSD_TICKET",defaultValue = "MyJsessionId") String ticket,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "30") Integer rows) {
		// 通过ticket获取用户
		Long shopid = null;
		User user = userService.queryUserByTicket(ticket);
		shopid = user.getId();
		// 根据shopid分页查询商品
		StsdaiResult<Item> stsdaiResult = this.itemService.queryItemByShopId(shopid, page, rows);
		return stsdaiResult;

	}
}
